package carassius;

import carassius.BLL.Settings;
import carassius.DAL.Database;
import carassius.GUI.MainForm;
import java.awt.Insets;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class CarassiusApp
		extends SingleFrameApplication
{
	private static File _confDir;

	/**
	 * At startup create and show the main frame of the application.
	 */
	@Override
	protected void startup()
	{
		createConfDir();
		//fix mac menu integration
		System.setProperty("apple.laf.useScreenMenuBar", "true");

		//set margins for buttons
		UIManager.put("ToggleButton.margin", new Insets(0, 0, 0, 0));
		UIManager.put("Button.margin", new Insets(0, 0, 0, 0));

		String dblocation = null;
		dblocation = Settings.getSetting("dblocation");

		if (dblocation == null)
		{
			StringBuilder sb = new StringBuilder();
			sb.append(_confDir.getPath());
			sb.append("/db");
			dblocation = sb.toString();
			Settings.setSetting("dblocation", dblocation);
		}

		initDatabase(dblocation);

		show(new MainForm());
	}

	/**
	 * This method is to initialize the specified window by injecting resources.
	 * Windows shown in our application come fully initialized from the GUI
	 * builder, so this additional configuration is not needed.
	 */
	@Override
	protected void configureWindow(java.awt.Window root)
	{
	}

	/**
	 * A convenient static getter for the application instance.
	 * @return the instance of CarassiusApp
	 */
	public static CarassiusApp getApplication()
	{
		return Application.getInstance(CarassiusApp.class);
	}

	private static void createConfDir()
	{
		StringBuilder sb = new StringBuilder();
		sb.append(System.getProperty("user.home"));
		sb.append("/.carassius/");

		_confDir = new File(sb.toString());
		if (!_confDir.exists())
		{
			_confDir.mkdir();
		}
		if (!Settings.getSettingsFile().exists())
		{
			try
			{
				Settings.getSettingsFile().createNewFile();
			}
			catch (IOException ex)
			{
				System.out.println(ex.toString());
			}
		}
	}

	private static void initDatabase(String dblocation)
	{
		try
		{
			Database.initDatabase(dblocation);
		}
		catch (ClassNotFoundException ex)
		{
			System.out.println(ex.toString());
			JOptionPane.showMessageDialog(null,
					ResourceBundle.getBundle("carassius.resources.Messages").getString("DatabaseLibraryInitError"),
					ResourceBundle.getBundle("carassius.resources.Messages").getString("DatabaseLibraryInitErrorTitle"),
					JOptionPane.ERROR_MESSAGE);
			System.exit(2);
		}
		catch (SQLException ex)
		{
			System.out.println(ex.toString());
			JOptionPane.showMessageDialog(null,
					ResourceBundle.getBundle("carassius.resources.Messages").getString("DatabaseInitError"),
					ResourceBundle.getBundle("carassius.resources.Messages").getString("DatabaseInitErrorTitle"),
					JOptionPane.ERROR_MESSAGE);
			System.exit(3);
		}
	}

	/**
	 * Main method launching the application.
	 */
	public static void main(String[] args)
	{
		launch(CarassiusApp.class, args);
	}
}
